
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_comm_base_framework/core/utils/ColorsUtil.dart';
import 'package:flutter_comm_base_framework/core/utils/style_util.dart';
import 'package:flutter_comm_base_framework/core/widgets/custom_text.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DialogUtil {

  Future<void> showConfirmDialog(BuildContext context,String title,String okTitle,String cancelTitle,Function(bool,String) goOnNext,{bool barrierDismissible = true,TextEditingController txfInput,TextInputType keyboardType,String hintText = ""}) async{
    TextEditingController txf = txfInput ?? TextEditingController();
    Map res = await showDialog<Map>(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) {
        return Dialog(child: Container(
          height: 170,
          child: Column(children: [
            Expanded(flex: 1,child: Container(child: Text(title,style: TextStyle(fontSize: 16,color: ColorsUtil.black_333),),padding: EdgeInsets.only(top: 20,left: 10,right: 10,bottom: 10),),),
            Container(padding: EdgeInsets.all(10), child: TextField(
              controller: txf,

              keyboardType: keyboardType,
              decoration: new InputDecoration(
                hintStyle: StyleUtil.textStyle.inptHintTextStyle,
                hintText: hintText,
                prefixIconConstraints:
                BoxConstraints(maxHeight: 30, maxWidth: 30),
              ),
              onChanged: (String value) {

              },
            ),),
            Container(height: 1,color: ColorsUtil.view_background,),
            Container(
              height: 45,child: Row(children:
            cancelTitle == null?[
              Expanded(flex: 1,child: GestureDetector(
                child: CustomText(okTitle,style: TextStyle(fontSize: 14,color: ColorsUtil.black_333),alignment: Alignment.center,),
                onTap: () {
                  //关闭对话框并返回true
                  Navigator.of(context).pop({"sure":true,"msg":txf.text});
                },
              ),),
            ]:
            [
              Expanded(flex: 1,child:  GestureDetector(
                child: CustomText(cancelTitle,style: TextStyle(fontSize: 14,color: ColorsUtil.black_333),alignment: Alignment.center),
                onTap: () => Navigator.of(context).pop({"sure":false,"msg":""}), // 关闭对话框
              ),),
              Container(width: 1,color: ColorsUtil.view_background,),
              Expanded(flex: 1,child: GestureDetector(
                child: CustomText(okTitle,style: TextStyle(fontSize: 14,color: ColorsUtil.btn_background),alignment: Alignment.center),
                onTap: () {
                  //关闭对话框并返回true
                  Navigator.of(context).pop({"sure":true,"msg":txf.text});
                },
              ),),
            ],),),
          ],),));
      },
    );
    bool isSure = res == null ? false:res["sure"];
    String msg = res == null ? "":res["msg"];
    goOnNext(isSure,msg);
  }


// 弹出对话框
  Future<void> showAlertDialog(BuildContext context,String title,String okTitle,String cancelTitle,Function(bool) goOnNext,{bool barrierDismissible = true}) async{
    bool isSure = await showDialog<bool>(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) {
        return Dialog(child: Container(
          height: 130,
          child: Column(children: [
            Expanded(flex: 1,child: Container(child: Text(title,style: TextStyle(fontSize: 16,color: ColorsUtil.black_333),),padding: EdgeInsets.only(top: 20,left: 10,right: 10,bottom: 10),),),
          Container(height: 1,color: ColorsUtil.view_background,),
          Container(
            height: 45,child: Row(children:
          cancelTitle == null?[
            Expanded(flex: 1,child: GestureDetector(
              child: CustomText(okTitle,style: TextStyle(fontSize: 14,color: ColorsUtil.black_333),alignment: Alignment.center),
              onTap: () {
                //关闭对话框并返回true
                Navigator.of(context).pop(true);
              },
            ),),
          ]:
          [
            Expanded(flex: 1,child:  GestureDetector(
              child: CustomText(cancelTitle,style: TextStyle(fontSize: 14,color: ColorsUtil.black_333),alignment: Alignment.center),
              onTap: () => Navigator.of(context).pop(false), // 关闭对话框
            ),),
            Container(width: 1,color: ColorsUtil.view_background,),
            Expanded(flex: 1,child: GestureDetector(
              child: CustomText(okTitle,style: TextStyle(fontSize: 14,color: ColorsUtil.btn_background),alignment: Alignment.center),
              onTap: () {
                //关闭对话框并返回true
                Navigator.of(context).pop(true);
              },
            ),),
          ],),),
        ],),));
      },
    );
    goOnNext(isSure??false);
  }

 Future<int> showListDialog(BuildContext context,List items,Function choose,{Function itemName}) async {
   var screenHeight = ScreenUtil.screenHeight;
    double height = min(45.0*(items.length).toDouble()+80.0 , screenHeight-160.0);
   int index = await showDialog<int>(
     context: context,
     builder: (BuildContext context) {
       var child =
       Container(
         margin: EdgeInsets.all(0),

         height: height,//(items.length < 10 ? 45.0*(items.length).toDouble()+60.0 : ScreenUtil.screenHeight-200.0),
         child: Column(
         children: <Widget>[
           ListTile(title: Text("请选择",textAlign: TextAlign.center,)),
           Expanded(
               child: ListView.builder(
                 itemCount: items.length,
                 itemBuilder: (BuildContext context, int index) {
                   return Container(
                     height: 45,
                     child: ListTile(
                     title: Text(itemName != null? itemName(items[index]):items[index],style: StyleUtil.textStyle.body1TextStyle,),
                     onTap: () => Navigator.of(context).pop(index),
                   ),);
                 },
               )),
         ],
       ),);
       //使用AlertDialog会报错
       return Dialog(child: child);
     },
   );
   if (index != null) {
     choose(items[index]);
   }
   return index;
 }
}